<template>
  <el-card class="notice-card">
    <div class="cfmEditorDiv">
      <el-form  ref="form" :model="notice" class="notice-editForm" label-position="top" label-width="20px">
        <el-form-item label="标题" class="title">
          <el-input v-model="notice.title" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="是否置顶">
          <el-switch v-model="notice.isTop"></el-switch>
        </el-form-item>
        <el-form-item label="公告等级">
          <el-select v-model="notice.level.codeValue" value-key="codeId" placeholder="请确认公告等级" @change="$forceUpdate"  clearable>
            <el-option v-for="item in noticeLevelList" :key="item.codeId" :label="item.codeName" :value="item.codeValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="预发布时间">
          <el-col :span="11">
            <el-date-picker type="datetime" placeholder="选择日期" v-model="notice.releaseDate" style="width: 100%;"></el-date-picker>
          </el-col>
          <el-button type="primary" class="submit-btn" @click="pushNotice">
            <span>确认发布</span>
          </el-button>
        </el-form-item>
        <el-form-item label="公告内容">
          <TEditor ref="editor" v-model="notice.content" />
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import TEditor from '@/components/tinyMce/TEditor.vue'
import { addNotice } from '@/services/index/notice/notice'
import { getAllCodeByType } from '@/services/util/util'
export default {
  components: {
    TEditor
  },
  data () {
    return {
      noticeLevelList: {},
      notice: {
        title: '',
        isTop: false,
        level: {},
        releaseDate: '',
        content: ''
      }
    }
  },
  created () {
    this.getLevel()
  },
  methods: {
    /**
     * 发布公告
     * */
    pushNotice () {
      const dataForm = {
        title: this.notice.title,
        isTop: this.notice.isTop,
        level: this.notice.level.codeValue,
        releaseDate: this.notice.releaseDate,
        content: this.notice.content
      }
      const data = JSON.stringify(dataForm)
      addNotice(data).then((res) => {
        if (res.code !== 10000) {
          this.$message.error('推送失败')
          return
        }
        this.$router.push('/portal/notice/home')
      })
    },
    /**
     * 获取公告等级
     * */
    getLevel () {
      const dataForm = {
        codeType: 'noticeLevel'
      }
      const data = JSON.stringify(dataForm)
      getAllCodeByType(data).then((res) => {
        if (res.code !== 10000) {
          this.$message.error('服务器开小差了')
          return
        }
        this.noticeLevelList = res.data.childCode
      })
    }
  }
}

</script>

<style scoped lang="less">
@import "pushNotice.less";
</style>
